﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["action"] == null) return;
        string act = Request.QueryString["action"].ToString();
        switch(act)
        {
            case "register":
                {
                    string s_username = Request.Form["username"].ToString();
                    string s_password = Request.Form["password"].ToString();
                    string s_qq = Request.Form["qqnumber"].ToString();
                    string s_email = Request.Form["email"].ToString();
                    string s_phone = Request.Form["telephone"].ToString();
                    string s_question = Request.Form["question"].ToString();
                    string s_answer = Request.Form["answer"].ToString();
                    if(CheckUsername(s_username)==false)
                    {
                        Response.Clear();
                        Response.Write("<script>alert('用户名已存在!')</script>");
                        return;
                    }
                    string sql = string.Format("insert into user (username,password,qq,email,telephone,problem,answer,privilege) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','0')", s_username, s_password, s_qq, s_email, s_phone, s_question, s_answer);
                    MysqlHelp dataprovider = new MysqlHelp();
                    int recall = dataprovider.Runsql(sql);
                    if (recall == -1)
                    {
                        Response.Clear();
                        Response.Write("<script>alert('注册失败！')</script>");
                    }
                    else
                    {
                        Response.Clear();
                        Response.Write("<script>alert('注册成功！')</script>");
                    }
                }
                break;
        }
    }
    protected bool CheckUsername(string username)
    {
        MysqlHelp dataprovider = new MysqlHelp();
        string sql = "select * from user where username = '" + username + "'";
        DataTable recall = dataprovider.GetDatabysql(sql);
        if (recall.Rows.Count == 0)
            return true;
        else return false;
    }
}
